//
//  PermissionsAlertView.swift
//  权限弹窗
//
//  Created by 千策万划-mini on 2025/5/2.
//

import Foundation

class PermissionsAlertView: UIView {
    
    var blockLeftClick:(() -> Void)?
    var blockRightClick:(() -> Void)?
    
    // MARK: - lazy
    lazy var imageBg: UIImageView = {
        let result = UIImageView()
        result.isUserInteractionEnabled = true
        result.widthHeight = XY(W(285), W(219))
        result.image = UIImage(named: "icon_permission_alert_bg")
        return result
    }()
    lazy var labelTitle: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var imageContentBg: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(251), W(84))
        result.image = UIImage(named: "icon_permission_alert_content_bg")
        return result
    }()
    lazy var labelContent: UILabel = {
        let result = UILabel()
        result.textAlignment = .center
        result.numberOfLines = 0
        result.textColor = COLOR_LABEL()
        result.font = UIFont.customMediumFont(size: 14)
        return result
    }()
    lazy var btnStop: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(118), W(46))
        result.tag = 1
        result.setBackgroundImage(UIImage(named: "icon_pop_confirm"), for: .normal)
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var labelStop: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var btnConfirm: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(118), W(46))
        result.tag = 2
        result.setBackgroundImage(UIImage(named: "icon_pop_cancel"), for: .normal)
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var labelConfirm: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT)
        self.backgroundColor = UIColor(hex: "000000", alpha: 0.8)
        
        self.addSubView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 添加
    func addSubView() {
        addSubview(imageBg)
        imageBg.addSubview(labelTitle)
        imageBg.addSubview(imageContentBg)
        imageContentBg.addSubview(labelContent)
        imageBg.addSubview(btnStop)
        imageBg.addSubview(btnConfirm)
        btnStop.addSubview(labelStop)
        btnConfirm.addSubview(labelConfirm)
        resetView()
    }
    
    // MARK: - 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        switch sender.tag {
        case 1:
            if let block = blockLeftClick {
                block()
            }else{
                hide()
            }
            break
        case 2:
            if let block = blockRightClick {
                block()
            }else{
                if let url = URL(string: UIApplication.openSettingsURLString) {
                    if UIApplication.shared.canOpenURL(url) {
                        UIApplication.shared.open(url, options: [:]) { success in
                            // 打开设置页面 = success
                            self.hide()
                        }
                    }
                }
            }
            break
        default:
            break
        }
        
        
        
    }
    
    // 显示
    func show() {
        if let window = WindowManager.shared.getCurrentWindow() {
            window.endEditing(true)
            // 设置初始状态（比如从底部开始）
            alpha = 0
            imageBg.transform = CGAffineTransform(translationX: 0, y: 50)
            window.addSubview(self)
            // 添加弹出动画
            UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) {
                self.alpha = 1
                self.imageBg.transform = .identity
            }
        }
    }

    // 隐藏
    func hide() {
        // 添加消失动画
        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn) {
            self.alpha = 0
            self.imageBg.transform = CGAffineTransform(translationX: 0, y: 50)
        } completion: { _ in
            self.removeFromSuperview()
            // 重置 transform，以便下次显示
            self.imageBg.transform = .identity
        }
    }
    
    // MARK: - 重置界面
    func resetView(model: ModelBtn = ModelBtn()) {
        labelTitle.resetView(
            title: UnPackStr(model.title),
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 16),
            shadowColor: UIColor(hex: "#4105A0"),
            outlineColor: UIColor(hex: "#3B0B9A")
        )
        labelTitle.centerXTop = XY(imageBg.width/2, W(12))
        
        labelContent.fitTitle(UnPackStr(model.content), variable: imageContentBg.width-W(14*2))
        imageContentBg.height = max(labelContent.height+W(10)*2, W(84))
        labelContent.centerXCenterY = XY(imageContentBg.width/2, imageContentBg.height/2)
        imageContentBg.centerXTop = XY(imageBg.width/2, labelTitle.bottom+W(5))
        
        btnStop.rightTop = XY(imageBg.width/2-W(6), imageContentBg.bottom+W(12))
        btnConfirm.leftCenterY = XY(imageBg.width/2+W(6), btnStop.centerY)
        labelStop.resetView(
            title: UnPackStr(model.leftTitle),
            textColor: COLOR_WHITE(),
            font: UIFont.customExtraBoldFont(size: 16),
            shadowColor: UIColor(hex: "#2D0084"),
            outlineColor: UIColor(hex: "#2D0084")
        )
        labelStop.centerXCenterY = XY(btnStop.width/2, btnStop.height/2)
        labelConfirm.resetView(
            title: UnPackStr(model.rightTitle),
            textColor: COLOR_WHITE(),
            font: UIFont.customExtraBoldFont(size: 16),
            shadowColor: UIColor(hex: "#2D0084"),
            outlineColor: UIColor(hex: "#2D0084")
        )
        labelConfirm.centerXCenterY = XY(btnConfirm.width/2, btnConfirm.height/2)
        
        imageBg.height = btnConfirm.bottom+W(16)
        imageBg.centerXCenterY = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT/2)
    }
}
